package com.work.game.home.ui.activity;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.Request;
import com.anythink.nativead.api.ATNativeAdView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.gyf.immersionbar.ImmersionBar;
import com.hjq.bar.OnTitleBarListener;
import com.hjq.bar.TitleBar;
import com.work.game.R;
import com.work.game.ad.MediationBannerAd;
import com.work.game.ad.MediationFeedAd;
import com.work.game.ad.MediationTablePlaqueAd;
import com.work.game.ad.kaku.banner.TaKuBannerAd;
import com.work.game.ad.kaku.banner.TaKuBannerAdOBJ;
import com.work.game.ad.kaku.interstitia.TaKuInterstitialAdGoodDetail;
import com.work.game.ad.kaku.interstitia.TaKuInterstitialAdTwo;
import com.work.game.ad.kaku.nativie.TaKuNativeAdGoodDetail;
import com.work.game.config.UrlConfig;
import com.work.game.home.ui.enums.RedisEnum;
import com.work.game.home.ui.model.Good;
import com.work.game.home.ui.model.vo.BaseVo;
import com.work.game.home.ui.model.vo.CollectVo;
import com.work.game.utils.GlideImageLoader;
import com.work.game.utils.StringSplit;
import com.work.game.volley.BaseActivity;
import com.work.game.volley.state.HUCState;
import com.zzhoujay.richtext.RichText;

import java.util.ArrayList;
import java.util.List;

import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.util.StrUtil;

public class GoodDetailActivity extends BaseActivity {
    private com.youth.banner.Banner banner;

    private TextView tvName;
    private TextView tvDescribe;
    private TextView tvCollect, tvLike, tvTitle;
    private ImageView collect, like;


    private Good good;
    private boolean collectFlag = false;
    private boolean likeFlag = false;

    private AlertDialog alertDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_good_detail_style);
        super.onCreate(savedInstanceState);
        initAd();
        initGoods();
    }

    @Override
    public void initView() {
        super.initView();
        ImmersionBar.with(this).titleBar(R.id.title).statusBarDarkFont(true).init();
       ;
        banner = findViewById(R.id.banner);
        tvName = findViewById(R.id.name);
        tvDescribe = findViewById(R.id.describe);
        tvCollect = findViewById(R.id.tv_collect);
        tvLike = findViewById(R.id.tv_like);
        collect = findViewById(R.id.collect);
        like = findViewById(R.id.like);
        tvTitle = findViewById(R.id.tv_title);
        TitleBar titleBar=findViewById(R.id.title);
        titleBar.setOnTitleBarListener(new OnTitleBarListener() {
            @Override
            public void onLeftClick(View view) {
                finish();
            }

            @Override
            public void onTitleClick(View view) {

            }

            @Override
            public void onRightClick(View view) {

            }
        });
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onSuccessfulStrType(int inputType, Object response) {
        super.onSuccessfulStrType(inputType, response);
        switch (inputType) {
            case HUCState.HUC_REQEUST_CODE_1:
                if (!(response instanceof Good)) {
                    return;
                }
                good = (Good) response;
                String wx = good.getPgWx();
                String money = good.getPgMoney();
                if (StrUtil.isNotEmpty(wx)) {
                    tvTitle.setText(money + "金币获取联系方式");
                    tvTitle.setVisibility(View.VISIBLE);
                    tvTitle.setOnClickListener(this);
                } else {
                    tvTitle.setVisibility(View.GONE);
                }
                List<String> picUrls = new ArrayList<String>();
                picUrls.add(good.getPgUrl());
                //设置图片加载器
//                banner.setImageLoader(new GlideImageLoader());
//                //设置图片集合
//                banner.setImages(picUrls);
//                banner.start();
                ImageView imageBanner=findViewById(R.id.image_banner);
                Glide.with(this)
                        .load(good.getPgUrl())
                        .apply(new RequestOptions().transform(new RoundedCorners(32)))
                        .into(imageBanner);
                tvName.setText(good.getPgTitle());
                RichText.fromHtml(good.getPgContent()).into(tvDescribe);
                //lick
                tvCollect.setText(good.getPgCount());
                tvLike.setText(good.getPgCountLike());
                String userIds = good.getUserIds();
                String userId = getSharedPreferences("id");
                if (CharSequenceUtil.isNotEmpty(userId) && userIds.contains(userId)) {
                    collect.setImageResource(R.drawable.ic_u4);
                    collectFlag = true;
                }
                String likeUserIds = good.getUserIdLikes();
                if (CharSequenceUtil.isNotEmpty(likeUserIds) && likeUserIds.contains(userId)) {
                    like.setImageResource(R.drawable.ic_u1);
                    likeFlag = true;
                }
                String userIdOrders = good.getUserIdOrders();
                if (CharSequenceUtil.isNotEmpty(userIdOrders) && userIdOrders.contains(userId)) {
                    tvTitle.setText("WX:" + good.getPgWx());
                    tvTitle.setOnClickListener(null);
                }
                collect.setOnClickListener(this);
                like.setOnClickListener(this);
                break;
            case HUCState.HUC_REQEUST_CODE_2:
                if (!(response instanceof String)) {
                    return;
                }
                String msg = (String) response;
                if (msg.contains("收藏")) {
                    if (collectFlag) {
                        collect.setImageResource(R.drawable.ic_u3);
                        tvCollect.setText(Integer.parseInt(good.getPgCount()) - 1 + CharSequenceUtil.EMPTY);
                        good.setPgCount(Integer.parseInt(good.getPgCount()) - 1 + CharSequenceUtil.EMPTY);
                    } else {
                        collect.setImageResource(R.drawable.ic_u4);
                        tvCollect.setText(Integer.parseInt(good.getPgCount()) + 1 + CharSequenceUtil.EMPTY);
                        good.setPgCount(Integer.parseInt(good.getPgCount()) + 1 + CharSequenceUtil.EMPTY);
                    }
                    collectFlag = !collectFlag;
                }
                showToast(msg);
                break;
            case HUCState.HUC_REQEUST_CODE_3:
                if (!(response instanceof String)) {
                    return;
                }
                String msgLike = (String) response;
                if (msgLike.contains("点赞")) {
                    if (likeFlag) {
                        like.setImageResource(R.drawable.ic_u2);
                        tvLike.setText(Integer.parseInt(good.getPgCountLike()) - 1 + CharSequenceUtil.EMPTY);
                        good.setPgCountLike(Integer.parseInt(good.getPgCountLike()) - 1 + CharSequenceUtil.EMPTY);
                    } else {
                        like.setImageResource(R.drawable.ic_u1);
                        tvLike.setText(Integer.parseInt(good.getPgCountLike()) + 1 + CharSequenceUtil.EMPTY);
                        good.setPgCountLike(Integer.parseInt(good.getPgCountLike()) + 1 + CharSequenceUtil.EMPTY);
                    }
                    likeFlag = !likeFlag;
                }
                showToast(msgLike);
                break;
            case HUCState.HUC_REQEUST_CODE_4:
                loadDismiss();
                if (!(response instanceof String)) {
                    return;
                }
                String msgOrder = (String) response;
                showToast(msgOrder);
                tvTitle.setText("WX:" + good.getPgWx());
                tvTitle.setOnClickListener(null);
                String moneyGoldCoin = getSharedPreferences("moneyGoldCoin");
                int moneyGoldCoinInt = Integer.parseInt(moneyGoldCoin) - Integer.parseInt(good.getPgMoney());
                setSharedPreferences("moneyGoldCoin", String.valueOf(moneyGoldCoinInt));
                break;
            default:
                break;
        }
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.collect:
                clickCollectOrLick(UrlConfig.URL_PRO_USER_COLLECT, HUCState.HUC_REQEUST_CODE_2);
                break;
            case R.id.like:
                clickCollectOrLick(UrlConfig.URL_PRO_USER_LIKE, HUCState.HUC_REQEUST_CODE_3);
                break;
            case R.id.tv_title:
                showGood();
                break;
            default:
                break;
        }
    }

    //加载详细
    private void initGoods() {
        Intent intent = getIntent();
        String id = (String) intent.getExtras().get("id");
        BaseVo baseVo = new BaseVo();
        baseVo.setId(id);
        hucRequestInterfase.executeStr(this, Good.class, HUCState.HUC_REQEUST_CODE_1, Request.Method.POST, UrlConfig.URL_PRO_GOOD, hucParameInterfase.getRequestParameter(baseVo), true);
    }

    //点击收藏
    private void clickCollectOrLick(String url, Integer code) {
        CollectVo collectVo = new CollectVo();
        collectVo.setPgId(good.getId());
        collectVo.setToken(getSharedPreferences("deviceId"));
        hucRequestInterfase.executeStr(this, null, code, Request.Method.POST, url, hucParameInterfase.getRequestParameter(collectVo), true);
    }

    //点击去顶
    private void clickCreateOrder() {
        loading();
        CollectVo collectVo = new CollectVo();
        collectVo.setPgId(good.getId());
        collectVo.setToken(getSharedPreferences("deviceId"));
        hucRequestInterfase.executeStr(this, null, HUCState.HUC_REQEUST_CODE_4, Request.Method.POST, UrlConfig.URL_PRO_USER_ORDER, hucParameInterfase.getRequestParameter(collectVo), true);
    }

    private void initAd() {
        ATNativeAdView flBanner = findViewById(R.id.fl_banner);
        String key = getSharedPreferences(RedisEnum.TA_KU.getKey());
        if (RedisEnum.TA_KU.getValue().equals(key)) {
            TaKuInterstitialAdGoodDetail taKuInterstitialAdGoodDetail = TaKuInterstitialAdGoodDetail.getInstance();
            taKuInterstitialAdGoodDetail.setToken(getSharedPreferences("deviceId"));
            taKuInterstitialAdGoodDetail.showAd(this);

            String code= StringSplit.splitOrNull(getSharedPreferences(RedisEnum.BANNER.getKey()), StrUtil.COMMA, 6);
            TaKuBannerAdOBJ taKuBannerAd1 = new TaKuBannerAdOBJ();
            taKuBannerAd1.setToken(getSharedPreferences("deviceId"));
            taKuBannerAd1.load(this,code, flBanner);
        } else {
            MediationTablePlaqueAd mediationTablePlaqueAd = MediationTablePlaqueAd.getInstance();
            mediationTablePlaqueAd.showCacheInterstitialFullAd(this, null);


            String code = StringSplit.splitOrNull(getSharedPreferences(RedisEnum.AD_INFORMATION_FLOW.getKey()), StrUtil.COMMA, 6);
            MediationFeedAd mediationFeedAdTop = new MediationFeedAd();
            mediationFeedAdTop.init(code);
            mediationFeedAdTop.setFlFeed(flBanner);
            mediationFeedAdTop.setActivity(this);
            mediationFeedAdTop.showCacheFeedAd();

        }
    }

    private void showGood() {
        //判断提现账号
        AlertDialog.Builder dialog = new AlertDialog.Builder(this);
        // dialog.setTitle("绑定姓名");
        View root = LayoutInflater.from(this).inflate(R.layout.pop_good_dateil, null);
        TextView tvContent = root.findViewById(R.id.tv_content);
        tvContent.setText("是否消费" + good.getPgMoney() + "金币获取WX");
        dialog.setView(root);
        root.findViewById(R.id.tv_ok).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
                clickCreateOrder();
            }
        });
        root.findViewById(R.id.tv_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
            }
        });
        alertDialog = dialog.create();
        alertDialog.getWindow().setBackgroundDrawableResource(R.color.transparent);
        alertDialog.setCancelable(false);
        alertDialog.show();
    }

}